package com.cloud.bc.controller.pc;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.controller.BaseController;
import com.cloud.bc.model.entity.*;
import com.cloud.bc.model.request.MomenRequest;
import com.cloud.bc.model.request.PcArticleRequest;
import com.cloud.bc.service.*;
import com.cloud.bc.util.JiaoYouPageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//PC单身
@Controller
@AllArgsConstructor
@RequestMapping("article")
public class PcArticleController extends BaseController {
    private final BcArticleCategorysService bcArticleCategorysService;
    private final BcArticlesService bcArticlesService;
    private final BcSinglesService bcSinglesService;
    private final BcSingleMatesService bcSingleMatesService;
    private final BcUserPhotosService bcUserPhotosService;
    private final BcSlideService bcSlideService;

    /**
     * 宝典
     */
    @GetMapping("")
    public ModelAndView article(HttpServletRequest request, HttpServletResponse response,
                                PcArticleRequest req) {
        ModelAndView view = baseView(request,"51找朋友-脱单宝典", "pc/article/index");
        view.addObject("categoryList", bcArticleCategorysService.selectAll(null));
        Page p = new Page();
        p.setCurrent(null == req.getPage() ? 1 : req.getPage());
//        p.setSize(10);//临时测试使用
        BcArticles bcArticles = new BcArticles();
        bcArticles.setCategoryId(req.getCategoryId());
        IPage<BcArticles> bcArticlesIPage = bcArticlesService.selectPage(p, bcArticles);
        JiaoYouPageHelper jiaoYouPageHelper = new JiaoYouPageHelper();
        view.addObject("page", jiaoYouPageHelper.getBcPageHTML(p, request));
        List<BcArticles> records = bcArticlesIPage.getRecords();
        view.addObject("articleList", records);
        view.addObject("cid", req.getCategoryId());
        //脱单宝典
        IPage<BcArticles> bcArticlesIPageRight = bcArticlesService.selectPage(new Page(1, 10), null);
        view.addObject("article", bcArticlesIPageRight.getRecords());
        return view;
    }

    /**
     * 宝典
     */
    @GetMapping("info/{id}")
    public ModelAndView article(HttpServletRequest request, HttpServletResponse response,
                                @PathVariable("id") Integer id) {
        BcArticles bcArticles = bcArticlesService.selectOne(new BcArticles().setId(id));
        if (null == bcArticles || Convert.toInt(bcArticles.getId(), 0) <= 0) {
            return new ModelAndView("redirect:/");
        }
        ModelAndView view = baseView(request,"51找朋友" + bcArticles.getTitle(), "pc/article/info");
        view.addObject("info", bcArticles);
        return view;
    }

}
